#!/bin/sh
# (c) syscl 2016-2019
# Handy script to mount EFI/ESP partition in macOS

#================================= GLOBAL VARS ==================================
BOLD="\033[1m"
RED="\033[1;31m"
GREEN="\033[1;32m"
BLUE="\033[1;34m"
OFF="\033[m"

# EFI identifier
gESPID=""

if [[ $# -eq 1 ]]; then
    # we mount it right here
    gESPID=$(echo "$@" | tr '[:upper:]' '[:lower:]')
else
    diskutil list
    printf "Enter EFI's IDENTIFIER, e.g. ${RED}disk0s1${OFF}"
    read -p ": " gESPID 
fi

# On macOS 10.14 Mojave, we have to be a sudoer
# to mount the EFI partition
let requireRootMinVer=14
# get system version
gProductVer="$(sw_vers -productVersion)"
gMINOR_VER=${gProductVer:3:2}
if [ $gMINOR_VER -ge $requireRootMinVer ]; then
    # 10.14+
    sudo diskutil mount ${gESPID}
else
    diskutil mount ${gESPID}
fi
# we must hold the return value from diskutil
gErrorStatus=$?

exit ${gErrorStatus}
